﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetImageCls
    {
        public string AssetListImageId;
        public string ImageFile;
        public string Ext;
        public int    IsAvatar;
    }


    public class AssetImageParser
    {
        public static AssetImageCls CreateInstance()
        {
            //tao 1 class trang....
            AssetImageCls oAssetImageCls = new AssetImageCls();
            return oAssetImageCls;
        }

        public static AssetImageCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0) return null;
            AssetImageCls oAssetImageCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetImageCls;
        }


        public static AssetImageCls ParseFromDataRow(DataRow dr)
        {
            AssetImageCls oAssetImageCls = new AssetImageCls();

            if (dr["AssetListImageId"] != DBNull.Value)
            {
                oAssetImageCls.AssetListImageId = (string)dr["AssetListImageId"];
            }
            if (dr["ImageFile"] != DBNull.Value)
            {
                oAssetImageCls.ImageFile = (string)dr["ImageFile"];
            }
            if (dr["Ext"] != DBNull.Value)
            {
                oAssetImageCls.Ext = (string)dr["Ext"];
            }
            if (dr["IsAvatar"] != DBNull.Value)
            {
                oAssetImageCls.IsAvatar = int.Parse(dr["IsAvatar"].ToString());
            }
           
            return oAssetImageCls;
        }

        public static AssetImageCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetImageCls[] AssetImageCls = new AssetImageCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetImageCls[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetImageCls;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("AssetImage");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetListImageId");
            ds.Tables["Info"].Columns.Add("ImageFile");
            ds.Tables["Info"].Columns.Add("Ext");
            ds.Tables["Info"].Columns.Add("IsAvatar");
            
            return ds;
        }

        public static string GetXml(AssetImageCls oAssetImageCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetImageCls.AssetListImageId,
                        oAssetImageCls.ImageFile,
                        oAssetImageCls.Ext,
                        oAssetImageCls.IsAvatar,
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetImageCls oAssetImage = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetImage);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetImageCls[] AssetImageCls = new AssetImageCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetImageCls[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetImageCls);
            return Xml;
        }


        public static string GetXml(AssetImageCls[] AssetImageCls)
        {
            if (AssetImageCls == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetImageCls = new AssetImageCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetImageCls.Length; iIndex++)
            {
                AssetImageCls oAssetImageCls = AssetImageCls[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                        oAssetImageCls.AssetListImageId,
                        oAssetImageCls.ImageFile,
                        oAssetImageCls.Ext,
                        oAssetImageCls.IsAvatar,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetImageCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetImageCls[0];
            }
            AssetImageCls[] AssetImageCls = new AssetImageCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetImageCls[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetImageCls;
        }
    }
}
